package com.reform.controller.common;


import com.reform.res.Result;
import com.reform.res.ResultCode;
import com.reform.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mortale
 * @since 2021-07-27
 */
@Api(tags = "FileController")
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    IFileService fileService;

    @ApiOperation(value = "上传文件")
    @PostMapping("/fileUpload")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", paramType = "query", dataTypeClass = MultipartFile.class, required = true),
    })
    public Result fileUpload(HttpServletRequest request, MultipartFile[] file) {
        return fileService.fileUpload(request,file);
    }

    @ApiOperation(value = "删除文件")
    @PostMapping("/fileDelete")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileNames", value = "文件名，json字符串，参数格式[{name:\"filename\"}]", paramType = "query", dataTypeClass = String.class, required = true),
    })
    public Result fileDelete(String fileNames) {
        return fileService.fileDelete(fileNames);
    }

    @ApiOperation(value = "下载文件")
    @GetMapping("/fileDownload")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileUrl", value = "文件路径", paramType = "query", dataTypeClass = String.class, required = true),
            @ApiImplicitParam(name = "fileName", value = "文件名", paramType = "query", dataTypeClass = String.class, required = true),
    })
    public Result fileDownload(HttpServletResponse response, String fileUrl, String fileName) {
        return fileService.fileDownload(response, fileUrl, fileName);
    }
}
